home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
boe.pres.k12.wv.us
/
boe.pres.k12.wv.us.zip
/
boe.pres.k12.wv.us
/
Utilities
/
Xerox Workcentre 5335
/
Windows Scan
/
32-bit_x86
/
Italiano
/
cpsimage.cab
/
data
/
xipProcs
/
hlExtract.proc
< prev
next >
Wrap
Text File
|
2009-03-16
|
7KB
|
208 lines
// Load scripted support procedures
#load "xipProcs/morphology.proc";
/* @XIPHighlightExtract
// DESCRIPTION
This procedure extracts highlighted text regions from a color image. It can
extract text regions for multiple highlighter colors, and return each text
extraction as a separate image or merge the extractions into a single image.
The resulting image(s) will be returned in the ELFLIST object "output".
// ARGUMENTS
XIPIMAGE input Image to extract highlighted text regions from
INTEGER merge This specifies the output format:
0 to keep each text extraction as a separate image
1 to combine each text extraction image using the AND
operator into a single text extraction image
// RETURNS
ELFLIST output This is a list of XIPIMAGE objects representing the
extracted highlighted text images. The info member of
each XIPIMAGE is a string indicating the highlighter
color(s) that covered the text.
*/
PROCEDURE XIPHighlightExtract (XIPIMAGE input, INTEGER merge)
RETURNS (ELFLIST output)
{
XIPIMAGE imgBase, imgText, imgHL, imgHLText;
STRING colorname, str, tmp;
STRING se5x5, se7x1;
INTEGER i, skipOne, nOutputs;
INTEGER minlevel, maxlevel;
INTEGER x, y, w, h;
ELFLIST win;
DOUBLE avg;
// Import IPCore definitions
#import "ipcore2xip.ucm";
// Create structuring element strings
se5x5 = XIPMakeSolidStrel (width: 5, height: 5).toString ();
se7x1 = XIPMakeSolidStrel (width: 7, height: 1).toString ();
// Find highlighter color regions
str = input.hlcinfo (
).toString (
).match (str: "color:"
);
// Process highlighter color regions
skipOne = 0;
for (tmp=str.getToken (); tmp; tmp=str.getToken ())
if (tmp == "color:") {
if (!skipOne) {
str.getLine ();
skipOne++;
} else {
// Parse highlighter color name
colorname = str.getToken ();
// Parse out region window
str.getToken ();
x = str.getToken ();
y = str.getToken ();
w = str.getToken ();
h = str.getToken ();
win = (x, y, w, h);
// Determine highlighter minimum and maximum levels
if (colorname == "orange") {
minlevel = 17;
maxlevel = 17;
} else if (colorname == "yellow") {
minlevel = 34;
maxlevel = 51;
} else if (colorname == "spring") {
minlevel = 68;
maxlevel = 68;
} else if (colorname == "green") {
minlevel = 85;
maxlevel = 85;
} else if (colorname == "teal") {
minlevel = 102;
maxlevel = 102;
} else if (colorname == "cyan") {
minlevel = 119;
maxlevel = 136;
} else if (colorname == "azure") {
minlevel = 153;
maxlevel = 153;
} else if (colorname == "blue") {
minlevel = 170;
maxlevel = 170;
} else if (colorname == "violet") {
minlevel = 187;
maxlevel = 187;
} else if (colorname == "magenta") {
minlevel = 204;
maxlevel = 238;
} else if (colorname == "pink") {
minlevel = 204;
maxlevel = 238;
} else if (colorname == "red") {
minlevel = 255;
maxlevel = 255;
}
imgBase = input.crop (window: win
).interleave (scanline: 1
).cspace (outspace: "hsv", precise: 1
);
// Threshold for text (text image)
imgText = imgBase.levels (levels: (12, 4, 4)
).median (filter: "2 2 1 1 1 1"
).range (lower: (0, 0, 0), upper: (255, 255, 85),
pixel: 1, incolor: (0, 0, 0), outcolor: (0, 0, 255)
).channel (parts: "3"
).convert (to: (1)
).dilate_ipc (
).erode_ipc (
);
// Threshold for highlighter color (highlighter image)
imgHL = imgBase.range (lower: (0, 32, 64), upper: (255, 255, 255),
pixel: 1, ingray: 1
).levels (levels: (12, 4, 4)
).median (filter: "2 2 1 1 1 1"
).range (lower: (minlevel, 85, 85),
upper: (maxlevel, 255, 255)
).channel (parts: "1"
).convert (to: (1)
).subsample (reduce: 4
).convert (to: (1)
).invert (
).dilate_ipc (sestring: se5x5
).erode_ipc (sestring: se5x5
).erode_ipc (
).dilate_ipc (
).scale (awindow: (win[2], win[3])
).convert (to: (1)
);
// Add text image to mask channel of highlighter image
imgHL = imgHL.addLayer (image: imgText, lindex: 0,
ltype: XIP_ColorMask);
// Seed fill to get the text covered by highlighter
imgHLText = imgHL.seedfill (iterations: 3);
// Continue if percent average is 100% (no highlighted text in region)
avg = getPercentAvg (inImg: imgHLText.convert (to: (8)));
if (avg == 1.00)
continue;
// Label extraction image
imgHLText.info = "Highlighter: " + colorname;
// Add extraction image to output list
output.insert (obj: imgHLText);
}
}
// Merge extraction images if merge option is specified
nOutputs = output.length ();
if (merge && (nOutputs > 1)) {
XIPIMAGE img, next;
STRING label;
img = output.extract (entry: 0);
for (i=1; i<nOutputs; i++) {
next = output.extract (entry: 0);
img = img.math (operator: "and", input: next);
label = next.info;
label.getToken ();
img.info = img.info + ", " + label.getToken ();
}
output.insert (obj: img);
}
}
/* @getPercentAvg
// DESCRIPTION
Given a text extraction image, return the average percent intensity. It uses
XIPIMAGE method "avginfo" to capture image parameters of min, max, and
average
*/
private
PROCEDURE getPercentAvg (XIPIMAGE inImg)
RETURNS (DOUBLE percent)
{
INTEGER j;
// Get average percent intensity of text extraction image
STRING info = inImg.avginfo ().toString ();
// Jump to "Entry 0" in "info" string then jump 13 tokens to get percent
info.match (str: "Entry 0:");
for (j=0; j<13; j++)
info.getToken ();
percent = info.getToken ();
}